package com.grid.action;
import java.io.StringReader;
import java.net.URI;
import java.sql.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.grid.model.Application;
import com.grid.model.Applications;
import com.grid.model.Operation;
import com.grid.model.Operations;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class GetDrillDownDetails extends ActionSupport {

	private String SelectedApplication;
	
	public String getSelectedApplication() {
		return SelectedApplication;
	}

	public void setSelectedApplication(String selectedApplication) {
		SelectedApplication = selectedApplication;
	}

	private String SelectedOperation;
	
	
	public String getSelectedOperation() {
		return SelectedOperation;
	}

	public void setSelectedOperation(String selectedOperation) {
		SelectedOperation = selectedOperation;
	}

	Map<String,Integer> DrillDownNameMapping = new LinkedHashMap<String, Integer>();
	
	

	public Map<String, Integer> getDrillDownNameMapping() {
		return DrillDownNameMapping;
	}

	public void setDrillDownNameMapping(Map<String, Integer> drillDownNameMapping) {
		DrillDownNameMapping = drillDownNameMapping;
	}


	private ArrayList<Operation> OperationList = new ArrayList<Operation>();
	

	public ArrayList<Operation> getOperationList() {
		return OperationList;
	}

	public void setOperationList(ArrayList<Operation> operationList) {
		OperationList = operationList;
	}


	public String execute(){
	
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		if(SelectedApplication != null)
		{
			session.put("SelectedApplication", SelectedApplication);
		}
		else
		{
			SelectedApplication = session.get("SelectedApplication").toString();
		}
		
		
	System.out.println("in execute of web service client drill down");
	ClientConfig config = new DefaultClientConfig();
	Client client = Client.create(config);
	WebResource service = client.resource(getBaseURI());
	ClientResponse response;

				
	response = service.path("rest").path("drilldown").path("application").path(SelectedApplication).path(SelectedOperation).get(ClientResponse.class);

		
	String xml = response.getEntity(String.class);
		
		
	Operations res = new Operations();
	    
	    try
	    {
		    JAXBContext ctx = JAXBContext.newInstance(Operations.class);
	        Unmarshaller um = ctx.createUnmarshaller();
	        res = (Operations)um.unmarshal(new StringReader(xml));
	    }
	    catch(Exception ex){
	    	System.out.println("jaxb exception occured");
	    }
        
	    OperationList = res.getOperationList();
	    
	    for(Operation temp : OperationList){
	    	DrillDownNameMapping.put(temp.getOperationName(), temp.getOperationValue());
	    }
	    

		return SUCCESS;
	}
	
	private static URI getBaseURI() {
	    return UriBuilder.fromUri("http://localhost:8080/HealthGridWebService").build();
	  }
	
}
